//
//  YJSalesTopicListController.m
//  SnackCourier
//
//  Created by qianfeng01 on 15/7/22.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJSalesTopicListController.h"

#import "YJSalesTopicListdetailsCell.h"
#import "YJFirstview.h"

#import "AFNetworking.h"
#import "YJSalesTopicListdetailsModel.h"
#import "UIImageView+WebCache.h"


#import "CreateSales.h"


@interface YJSalesTopicListController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UICollectionView *colleCtionView;

@property (nonatomic,copy)NSString *imgUrl;

@end

@implementation YJSalesTopicListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavigationItem];
    self.title =self.model.title;
    
    self.imgUrl =[[NSString alloc] init];
    self.dataArray =[[NSMutableArray alloc] init];
    NSString *urlStr =[NSString stringWithFormat:kSalespromotionUrl,(long)self.model.id];
    [self createHttpRequest];
    [self initUI];
    [self loadData:urlStr];
    
}

-(void)initUI{
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection =UICollectionViewScrollDirectionVertical;
    flowLayout.minimumLineSpacing =10;
    flowLayout.itemSize =CGSizeMake((self.view.frame.size.width-10)/2.0, 260);
    self.automaticallyAdjustsScrollViewInsets =NO;
    self.colleCtionView =[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-55) collectionViewLayout:flowLayout];
    self.colleCtionView.dataSource=self;
    self.colleCtionView.delegate=self;
    self.colleCtionView.backgroundColor=[UIColor colorWithWhite:0.9 alpha:1];;
    
    [self.colleCtionView registerNib:[UINib nibWithNibName:@"YJSalesTopicListdetailsCell" bundle:nil] forCellWithReuseIdentifier:@"YJSalesTopicListdetailsCell"];
    [self.colleCtionView registerClass:[YJFirstview class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YJFirstview"];
    [self.view addSubview:self.colleCtionView];
}


-(void)loadData:(NSString *)urlStr{
    __weak typeof (self)weakSelf=self;
    [self.manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dicts =[NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *data =dicts[@"data"];
            weakSelf.imgUrl =data[@"picUrl"];

            
            NSArray *array =data[@"productList"];
            for (NSDictionary *dict in array) {
                YJSalesTopicListdetailsModel *model =[[YJSalesTopicListdetailsModel alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [weakSelf.dataArray addObject:model];
            }
            [weakSelf.colleCtionView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YJSalesTopicListdetailsCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"YJSalesTopicListdetailsCell" forIndexPath:indexPath];
    YJSalesTopicListdetailsModel *model =self.dataArray[indexPath.row];
    [cell showDataWithModel:model];
    cell.backgroundColor =[UIColor whiteColor];
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view =nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"YJFirstview" forIndexPath:indexPath];
        UIImageView *imageView =[[UIImageView alloc] initWithFrame:view.frame];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.imgUrl]];
        [view addSubview:imageView];
    }
    return view;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 80);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YJSalesTopicListdetailsModel *model =self.dataArray[indexPath.row];
    YJImmediatelygrabController *controller =[CreateSales createVC:(long)model.id];
    controller.title =@"商品详情";
    [self.navigationController pushViewController:controller animated:YES];
    
}






@end
